﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace DataAccess.Util
{
    public class ReflectUtils
    {
        /// <summary>
        /// Get mapping from Entity to databse column.
        /// </summary>
        /// <returns>mapping dictionary</returns>
        public static Dictionary<string, string> GetColumnMapping(Type objType)
        {
            Dictionary<string, string> columnMapping = new Dictionary<string, string>();
            foreach (PropertyInfo info in objType.GetProperties())
            {
                object[] objAttrs = info.GetCustomAttributes(typeof(ColumnNameAttribute), true);
                foreach (Attribute attr in objAttrs)
                {
                    if (attr is ColumnNameAttribute)
                    {
                        ColumnNameAttribute a = attr as ColumnNameAttribute;
                        string name = string.IsNullOrEmpty(a.GetName()) ? NameUtils.GetDefaultColumnNameByPropertyName(info.Name) : a.GetName();
                        columnMapping.Add(info.Name, name);
                    }
                }
            }

            return columnMapping;
        }
    }
}
